﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;
using System.ComponentModel;

namespace TA.Turbo.Controls
{
	/// <summary>
	/// A hyper link.
	/// </summary>
	public class Link : BaseSimpleValueControl
	{
		#region STATIC
		/// <summary>
		/// Adds HREF and HREFJS attributes depending on <see cref="TPage.JavaScript"/> status.
		/// </summary>
		/// <param name="writer"></param>
		/// <param name="ctl"></param>
		/// <param name="href"></param>
		/// <param name="hrefJs"></param>
		public static void AddHrefAttribute(HtmlTextWriter writer, Control ctl, string href, string hrefJs)
		{
			if (!ctl.get_IsDesignMode())
			{
				if (ctl.get_TPage().JavaScript == true)
				{
					if (!string.IsNullOrEmpty(hrefJs))
						writer.AddAttribute(HtmlTextWriterAttribute.Href, hrefJs);
					else if (!string.IsNullOrEmpty(href))
						writer.AddAttribute(HtmlTextWriterAttribute.Href, ctl.ResolveUrl(href));
				}
				else if (ctl.get_TPage().JavaScript == false)
				{
					if (!string.IsNullOrEmpty(href))
						writer.AddAttribute(HtmlTextWriterAttribute.Href, ctl.ResolveUrl(href));
					else
					{
						if (!string.IsNullOrEmpty(hrefJs))
							writer.AddAttribute(HtmlTextWriterAttribute.Href, hrefJs.Replace("#", ctl.get_TPage().GetReplacementForHash()));
					}
				}
				else
				{
					if (!string.IsNullOrEmpty(href))
						writer.AddAttribute(HtmlTextWriterAttribute.Href, ctl.ResolveUrl(href));
					else
					{
						if (!string.IsNullOrEmpty(hrefJs))
							writer.AddAttribute(HtmlTextWriterAttribute.Href, hrefJs.Replace("#", ctl.get_TPage().GetReplacementForHash()));
					}

					if (!string.IsNullOrEmpty(hrefJs))
						writer.AddAttribute("hrefjs", hrefJs);
				}
			}
		}
		#endregion
		#region PUBLIC
		#region INIT
		/// <summary>
		/// Ctor.
		/// </summary>
		public Link()
		{
			this.Tag = HtmlTextWriterTag.A;
		}
		#endregion
		#region PROPS
		#region DEF
		/// <summary>
		/// Props.
		/// </summary>
		protected static new Properties _clientProperties = Define(BaseValueDisplayControl._clientProperties,
			new BasePropertyDefinition[] {
				new PropertyDefinition<string>("Text", "Tx", null) { IsUsedByClient = false, },
				new UrlPropertyDefinition("Href", "H", null) { IsHTML = true, IsUsedByClient = false, },
				new UrlPropertyDefinition("HrefJS", "HJ", null) { IsHTML = true, IsUsedByClient = false, },
			});
		/// <summary>
		/// Props.
		/// </summary>
		public override Properties ClientProperties
		{
			get
			{
				return _clientProperties;
			}
		}
		#endregion
		[DefaultValue(HtmlTextWriterTag.A)]
		public override HtmlTextWriterTag Tag
		{
			get
			{
				return base.Tag;
			}
			set
			{
				base.Tag = value;
			}
		}
		/// <summary>
		/// Gets or sets the text to be displayed inside this control.
		/// </summary>
		[Category("Appearance")]
		public string Text
		{
			get
			{
				return GetProperty<string>("Text");
			}
			set
			{
				ViewState["Text"] = value;
			}
		}
		/// <summary>
		/// The HREF to be output if <see cref="TPage.JavaScript"/> is not true (that is, false or null).
		/// </summary>
		public string Href
		{
			get
			{
				return GetProperty<string>("Href");
			}
			set
			{
				ViewState["Href"] = value;
			}
		}
		/// <summary>
		/// This is output as HREFJS if <see cref="TPage.JavaScript"/> is null or as HREF if <see cref="TPage.JavaScript"/> is true.
		/// </summary>
		public string HrefJS
		{
			get
			{
				return GetProperty<string>("HrefJS");
			}
			set
			{
				ViewState["HrefJS"] = value;
			}
		}
		/// <summary>
		/// Non-typed value object.
		/// </summary>
		public override object GenericValue
		{
			get
			{
				return this.Text;
			}
			set
			{
				this.Text = (string)value;
			}
		}
		#endregion
		#endregion
		#region INTERNAL
		#region PROPS
		/// <summary>
		/// Gets or sets the <see cref="Text"/> property.
		/// </summary>
		protected internal override string SerializedValue
		{
			get
			{
				return this.Text;
			}
			set
			{
				this.Text = value;
			}
		}
		#endregion
		#region METHODS
		#region INIT
		/// <summary>
		/// Registers LinkControl.js.
		/// </summary>
		protected internal override void RegisterIncludes(IncludeCollection includes)
		{
			base.RegisterIncludes(includes);

			includes.Add(new ScriptResourceInclude("TA.Turbo.Res.JS.Simple.LinkControl.js", typeof(Image)));
		}
		#endregion
		/// <summary>
		/// Adds the HREF attributes using <see cref="Link.AddHrefAttribute"/> methods.
		/// </summary>
		/// <param name="writer"></param>
		protected override void AddAttributesToRender(System.Web.UI.HtmlTextWriter writer)
		{
			Link.AddHrefAttribute(writer, this, this.Href, this.HrefJS);
			base.AddAttributesToRender(writer);
		}
		/// <summary>
		/// Renders the contents (which are the string stored in <see cref="Text"/> property).
		/// </summary>
		/// <param name="writer"></param>
		protected override void RenderContents(System.Web.UI.HtmlTextWriter writer)
		{
			base.RenderContents(writer);

			writer.Write(this.Text);

			if (this.get_IsDesignMode())
			{
				if (string.IsNullOrEmpty(this.Text))
					writer.Write(this.GetType().Name);
			}
		}
		#endregion
		#endregion

	}
}
